<?php

class ChamadoController extends TWin8 {

    private $logic;

    public function __construct() {
        parent::__construct();
        $this->logic = new ChamadoLogic();
    }

    public function index() {
        $this->REDIRECT->goToAction('listar');
    }

    public function listar() {
        
        $this->addDados('toolbar', TWin8Helper::displayToolBar(array('novo')));
        
        $arrayObjChamado = $this->logic->listar(null,null,true);
        $this->addDados('listChamado', $arrayObjChamado);
        $this->TPageSecondary('lista');
        
    }
    
    
    public function cadastrar(){
        
        $this->HTML->addJavaScript(PATH_JS_CORE_URL . "jquery.validate.js");
        $this->HTML->addJavaScript(PATH_JS_CORE_URL . "mask.js");
        $this->HTML->addJavaScript(PATH_JS_URL . $this->_controller . "/" . $this->_action . ".js");
        
        $objLogicGrupo = new GrupoLogic();
        $this->addDados("listGrupo", TFormHelper::optionSelect($objLogicGrupo->listar(null,'descricao'),'descricao'));

        $objVColaboradorLogic = new VColaboradorLogic();
        $this->addDados("listColaborador", TFormHelper::optionSelect($objVColaboradorLogic->listar(null,'nome')));
        
        
        $objLogicProblema = new ProblemaLogic();
        $this->addDados("listProblema", TFormHelper::optionSelect($objLogicProblema->listar(null,'descricao'),'descricao'));
        
        $this->TPageSecondary('cadastro');
    }
    
    public function inserir() {
        
        if ($_SERVER['REQUEST_METHOD'] == "POST") {

            $_POST['dataCriacao'] = time();
            $_POST['idUsuarioCriador'] = $this->SECURITY->getUsuario()->getId();

            $salvar = $this->logic->salvar($_POST, true);

            if ($salvar[0]) {
                $this->REDIRECT->goToAction("listar");
            } else {
                #Retornar erros;
            }
        } else {
            $this->REDIRECT->goToAction("cadastrar");
        }
    }
    
    
    public function informar() {

        if (!$this->isParam('id')) {
            $this->REDIRECT->goToAction("listar");
        }

        $this->HTML->addJavaScript(PATH_JS_CORE_URL . "MetroUI/pagecontrol.js");
        $this->addDados('toolbar', TWin8Helper::displayToolBar(array('editar')));
        $this->addDados("objChamado", $this->logic->obterPorId($this->getParam('id'),true));

        $this->TPageSecondary('informa');
    }
    
    
    public function editar() {
        
        $this->HTML->addJavaScript(PATH_JS_CORE_URL . "jquery.validate.js");
        $this->HTML->addJavaScript(PATH_JS_CORE_URL . "jquery.populate.js");
        $this->HTML->addJavaScript(PATH_JS_URL . $this->_controller . "/" . $this->_action . ".js");
        
        $objLogicGrupo = new GrupoLogic();
        $this->addDados("listGrupo", TFormHelper::optionSelect($objLogicGrupo->listar(null,'descricao'),'descricao'));

        $objLogicProblema = new ProblemaLogic();
        $this->addDados("listProblema", TFormHelper::optionSelect($objLogicProblema->listar(null,'descricao'),'descricao'));
        
        $this->addDados('json_objeto', $this->logic->objectToJson( $this->logic->obterPorId($this->getParam('id')) ));

        $this->TPageSecondary('edita');
    }
    
    
    public function atualizar() {

        $_POST['idUsuarioAtualizador'] = $this->SECURITY->getUsuario()->getId();
        $_POST['dataAtualizacao'] = time();

        $salvar = $this->logic->salvar($_POST, TRUE);

        if ($salvar[0]) {
            $this->REDIRECT->setUrlParameter("id", $_POST['id']);
            $this->REDIRECT->goToAction('informar');
        } else {
           
        }
    }
        
    

   
    
}

?>